<template>
  <div center>
    <el-form ref="info2" :inline="true" :rules="rules" :model="info" size="mini" label-position="right"
             label-width="110px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="受检单位" prop="sjcdwmc">
            <el-input v-model="info.sjcdwmc" placeholder="请输入内容" :disabled="true"/>
          </el-form-item>
          <el-form-item label="检测编号" prop="jcbh">
            <el-input v-model="info.jcbh" placeholder="请输入内容" :disabled="true"/>
          </el-form-item>
          <el-form-item label="检测类型" prop="jclx">
            <el-input v-model="info.jclx" placeholder="请输入内容" :disabled="isShow"/>
          </el-form-item>

        </el-col>
        <el-col :span="8">
          <el-form-item label="测量日期" prop="cyrq">
            <el-date-picker
              v-model="info.cyrq"
              :disabled="isShow"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="测量仪器" prop="cyyq">
            <el-select v-model="info.cyyq" filterable placeholder="请选择">
              <el-option
                v-for="item in cyyqList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <!--            <el-input v-model="info.cyyq" placeholder="请输入内容" @change="getYqBh()" :disabled="isShow"/>-->
          </el-form-item>

        </el-col>
        <el-col :span="8">
          <el-form-item label="测量方法" prop="cyyq">
            <el-input v-model="info.clff" placeholder="请输入内容" :disabled="isShow"/>
          </el-form-item>
          <el-form-item label="仪器编号" prop="yqbh">
            <el-select v-model="info.yqbh" filterable placeholder="请选择" clearable>
              <el-option
                v-for="item in cyyqList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <!--            <el-input v-model="info.yqbh" placeholder="请输入内容" :disabled="isShow"/>-->
          </el-form-item>
        </el-col>
      </el-row>

      <el-divider></el-divider>
      <el-form-item v-if="!isShow">
        <el-button type="warning" size="mini" @click="addTable1(null,null)">添加</el-button>
      </el-form-item>
      <el-table key="table1" border :data="info.table1Json" style="width: 100%">
        <el-table-column
          prop="measurementWorkshop"
          align="center"
          label="测量岗位">
        </el-table-column>
        <el-table-column
          prop="measurementArea"
          align="center"
          label="测量地点">
        </el-table-column>
        <el-table-column
          prop="inspectionTime"
          align="center"
          label="工人巡检停留时间/巡检次数">
        </el-table-column>
        <el-table-column label="工作场所气象条件测量结果" align="center">
          <el-table-column label="温度(℃)" align="center">
            <el-table-column
              prop="resultsValueWd1"
              align="center"
              label="测量值">
            </el-table-column>
            <el-table-column
              prop="resultsValueWd2"
              align="center"
              label="修正结果">
            </el-table-column>
          </el-table-column>
          <el-table-column label="相对湿度(%)" align="center">
            <el-table-column
              prop="resultsValueSd1"
              align="center"
              label="测量值">
            </el-table-column>
            <el-table-column
              prop="resultsValueSd2"
              align="center"
              label="修正结果">
            </el-table-column>
          </el-table-column>
          <el-table-column label="气压(kPa)" align="center">
            <el-table-column
              prop="resultsValueQy1"
              align="center"
              label="测量值">
            </el-table-column>
            <el-table-column
              prop="resultsValueQy2"
              align="center"
              label="修正结果">
            </el-table-column>
          </el-table-column>
          <el-table-column label="风速(m/s)" align="center">
            <el-table-column
              prop="resultsValueFs1"
              align="center"
              label="测量值">
            </el-table-column>
            <el-table-column
              prop="resultsValueFs2"
              align="center"
              label="修正结果">
            </el-table-column>
          </el-table-column>
        </el-table-column>
        <el-table-column
          prop="note"
          align="center"
          label="备注">
        </el-table-column>
        <el-table-column
          align="center"
          fixed="right"
          label="操作" v-if="!isShow">
          <template slot-scope="scope">
            <el-button @click="addTable1(scope.$index,scope.row)" type="text" size="small">编辑</el-button>
            <el-button type="text" size="small" @click="delTable1(scope.$index,scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-divider></el-divider>
      <el-form-item label="测量人" prop="cyr">
        <el-select v-model="info.cyr" :disabled="isShow" multiple placeholder="请选择" :multiple-limit="2">
          <el-option
            v-for="item in userList"
            :label="item.nickName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="校对人" prop="jdr">
        <el-select v-model="info.jdr" :disabled="isShow" multiple placeholder="请选择" :multiple-limit="2">
          <el-option
            v-for="item in userList"
            :label="item.nickName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="受检单位陪同人" prop="sjdwptr">
        <el-input type="text" autosize v-model="info.sjdwptr" placeholder="打印后手签" disabled/>
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <div style="text-align: center" v-if="!isShow">
      <el-button type="primary" size="small" @click="addOrEdit">保存</el-button>
    </div>
    <el-dialog :visible.sync="centerDialogVisible" append-to-body center width="80%">
      <el-form ref="tableInfo" :inline="true" :rules="rules" :model="tableInfo" size="mini" label-position="right"
               label-width="200px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="测量岗位" prop="measurementWorkshop">
              <el-input v-model="tableInfo.measurementWorkshop" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item label="测量地点" prop="measurementArea">
              <el-input v-model="tableInfo.measurementArea" placeholder="请输入内容"/>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="工人巡检停留时间/巡检次数" prop="inspectionTime">
              <el-input v-model="tableInfo.inspectionTime" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item label="备注" prop="note">
              <el-input v-model="tableInfo.note" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>工作场所气象条件测量结果-温度(℃)</span>
          </div>
          <el-form-item label="测量值" prop="resultsValueWd1">
            <el-input v-model="tableInfo.resultsValueWd1" placeholder="请输入内容"/>
          </el-form-item>
          <el-form-item label="修正结果" prop="resultsValueWd2">
            <el-input v-model="tableInfo.resultsValueWd2" placeholder="请输入内容"/>
          </el-form-item>
        </el-card>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>工作场所气象条件测量结果-相对湿度(%)</span>
          </div>
          <el-form-item label="测量值" prop="resultsValueSd1">
            <el-input v-model="tableInfo.resultsValueSd1" placeholder="请输入内容"/>
          </el-form-item>
          <el-form-item label="修正结果" prop="resultsValueSd2">
            <el-input v-model="tableInfo.resultsValueSd2" placeholder="请输入内容"/>
          </el-form-item>
        </el-card>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>工作场所气象条件测量结果-气压(kPa)</span>
          </div>
          <el-form-item label="测量值" prop="resultsValueQy1">
            <el-input v-model="tableInfo.resultsValueQy1" placeholder="请输入内容"/>
          </el-form-item>
          <el-form-item label="修正结果" prop="resultsValueQy2">
            <el-input v-model="tableInfo.resultsValueQy2" placeholder="请输入内容"/>
          </el-form-item>
        </el-card>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>工作场所气象条件测量结果-风速(m/s)</span>
          </div>
          <el-form-item label="测量值" prop="resultsValueFs1">
            <el-input v-model="tableInfo.resultsValueFs1" placeholder="请输入内容"/>
          </el-form-item>
          <el-form-item label="修正结果" prop="resultsValueFs2">
            <el-input v-model="tableInfo.resultsValueFs2" placeholder="请输入内容"/>
          </el-form-item>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="centerDialogVisible = false">关 闭</el-button>
        <el-button type="primary" size="mini" @click="addOrEditTable1">确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import imgUpdate from '@/components/Upload/imgUpdate'
import { getInfo, addOrEdit } from '@/api/table/tableFour'
import { getUserList } from '@/api/system/sysUser'
import { getUserId } from '@/utils/auth'
import { getList } from '@/api/system/protectiveWear'

export default {
  components: {
    imgUpdate
  },
  props: {
    noCreated: {
      type: Boolean,
      default: null
    },
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      info: {},
      userId: getUserId(),
      userList: [],
      tableInfo: {},
      cyyqList: [],//采样仪器
      tableIndex: null,
      table1Id: null,
      centerDialogVisible: false,
      centerDialogVisible2: false,
      rules: {
        dzr: [{ required: true, message: '请输入内容', trigger: 'chang' }],
        sjcdwptr: [{ required: true, message: '请输入内容', trigger: 'chang' }],
        cyrq: [{ required: true, message: '请选择日期', trigger: 'chang' }],
        dzrq: [{ required: true, message: '请选择日期', trigger: 'chang' }],
        shr: [{ required: true, message: '请选择内容', trigger: 'chang' }],
        cyr: [{ required: true, message: '请选择内容', trigger: 'chang' }],
        jdr: [{ required: true, message: '请选择内容', trigger: 'chang' }],
        bgxx: [{ required: true, message: '请选择内容', trigger: 'chang' }]
      }
    }
  },
  created() {
    this.getUserList()
    this.info.table1Json = []
    // this.info.table2Json = []
  },
  methods: {
    getYqBh() {
      getList(null, null, null, this.info.cyyq).then(response => {
        if (response.code == 0) {
          this.cyyqList = []
          response.data.forEach(item => {
            this.cyyqList.push({
              value: item.number,
              label: item.number
            })
          })
        }
      })
    },
    // 新增或编辑表1
    addTable1(index, row) {
      this.tableIndex = index
      this.tableInfo = {}
      if (row != null) {
        this.tableInfo = row
      }
      this.centerDialogVisible = true
    },
    // 保存表1
    addOrEditTable1() {
      if (this.tableIndex == null) {
        this.info.table1Json.push(this.tableInfo)
      } else {
        this.info.table1Json.splice(this.tableIndex, 1, this.tableInfo)
      }
      this.centerDialogVisible = false
    },
    // 删除表1
    delTable1(index, row) {
      this.info.table1Json.splice(index, 1)
    },
    // 获取所有用户
    getUserList() {
      getUserList().then(response => {
        this.userList = response.data
      })
    },
    getInfo(id, table1Id) {
      getInfo(id, table1Id).then(response => {
        if (response.data.table1Json != null && response.data.table1Json != '') {
          response.data.table1Json = JSON.parse(response.data.table1Json)
        } else {
          response.data.table1Json = []
        }
        if (response.data.jdr != null) {
          response.data.jdr = JSON.parse(response.data.jdr)
          var ids = []
          for (let i = 0; i < response.data.jdr.length; i++) {
            ids.push(response.data.jdr[i].id)
          }
          response.data.jdr = ids
        } else {
          response.data.jdr = []
        }
        if (response.data.cyr != null) {
          response.data.cyr = JSON.parse(response.data.cyr)
          var ids = []
          for (let i = 0; i < response.data.cyr.length; i++) {
            ids.push(response.data.cyr[i].id)
          }
          response.data.cyr = ids
        } else {
          response.data.cyr = []
        }
        this.info = response.data
        this.getYqBh()
      })
    },
    addOrEdit: function() {
      this.$refs['info2'].validate((valid) => {
        if (valid) {
          addOrEdit(this.info.id, this.info.tableOneId, this.info.cyrq, JSON.stringify(this.info.jdr), JSON.stringify(this.info.cyr), this.info.sjdwptr,
            JSON.stringify(this.info.table1Json), 18, this.info.jcxm, this.info.jclx, this.info.cyyj, this.info.qy, this.info.fs, this.info.cyyq, this.info.xdsd, this.info.wd, this.info.yqbh, this.info.sjjzqxh, this.info.jzz, this.info.clff, this.info.bgxx).then(response => {
            if (response.code != 0) {
              return
            }
            if (this.info.id == null) {
              // 新增
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            } else {
              // 编辑
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            }
            this.$parent.$parent.getData()
            this.$parent.$parent.centerDialogVisible = false
          })
        }
      })
    },
    // 初始化
    init(id, table1Id) {
      this.getInfo(id, table1Id)
    }
  }
}
</script>

